import string

from Tkinter import *


class ListboxWrapper(Frame):
    def __init__(self, master, color, high):
        Frame.__init__(self, master)
        self.master = master
        
        self.scrollbarY = Scrollbar(master, bg=color, orient=VERTICAL)
        self.scrollbarX = Scrollbar(master, bg=color, orient=HORIZONTAL)

        self.listbox = Listbox(master, background=color,\
                               yscrollcommand=self.scrollbarY.set, \
                               xscrollcommand=self.scrollbarX.set, \
                               height=high, \
                               width=20,\
                               relief=FLAT,\
                               borderwidth=2,\
                               selectmode=MULTIPLE)
        
        self.removeButton = Button(master, background=color, text="Remove", \
                                   width = 8)
        self.clearButton = Button(master, background=color, text="Clear", \
                                  width = 8)     
        
        self.listbox.grid(row=0, column=0, columnspan=2)
        self.scrollbarY.grid(row=0, column=2)
        self.scrollbarX.grid(row=1, column=0)
        self.removeButton.grid(row=2, column=1)
        self.clearButton.grid(row=2, column=2)

        self.scrollbarY.config(command=self.listbox.yview)
        self.scrollbarX.config(command=self.listbox.xview)
        self.removeButton.config(command=self.removeCurrentSelection)
        self.clearButton.config(command=self.removeAll)
                        
        self.allItems = []
        self.selectedItems = []  


    def addItems(self, items):
        for item in items:
            self.allItems.append(item)
            self.listbox.insert(END, item)
                
    #maybe remove this
    def removeItems(self ,items):
        for item in items:
            index = self.allItems.index(item)
            self.listbox.delete(index)
            self.allItems.remove(item)
      

    def removeAll(self):
        self.allItems = []
        self.listbox.delete(0, END)

    def saveCurrentSelection(self):
        self.selectedItems = []
        indexes = map(string.atoi, self.listbox.curselection())
        for index in indexes:
            self.selectedItems.append(self.allItems[index])
    
    def getSelection(self):
        return self.selectedItems

    def getAllItems(self):
        return self.allItems

    def removeCurrentSelection(self):
        indexes = map(string.atoi, self.listbox.curselection())
        counter = 0
        for index in indexes:
            newIndex = index - counter
            self.listbox.delete(newIndex)
            self.allItems.remove(self.allItems[newIndex])
            counter += 1
               


